package summer.hot100;

public class Solution19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        while (--n > 0) {
            head = head.next;
        }
        ListNode p = dummy;
        while (head.next != null) {
            p = p.next;
            head = head.next;
        }
        p.next = p.next.next;
        return dummy.next;
    }
}


class ListNode {
    int val;
    ListNode next;

    public ListNode() {
    }

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}